package com.livrexpress.livrexpressandroid.localdomain;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.DatabaseUtils;
import android.database.sqlite.SQLiteDatabase;
import android.provider.MediaStore.Images;

public class Delivery implements Entity{
	public static final String STATE_TODELIVER = "toDeliver";
	public static final String STATE_DELIVERED = "delivered";
	public static final String STATE_NOTDELIVERED = "notDelivered";
	
	private static String TABLE = "Delivery";
	private static String COL_ID_DELIVERY = "idDelivery";
	public static int COL_ID_DELIVERY_NUM = 0;
	private static String COL_ID_TOUR = "idTour";
	public static int COL_ID_TOUR_NUM = 1;
    private static String COL_ID_SENDER = "idSender";
    public static int COL_ID_SENDER_NUM = 2;
    private static String COL_DATEDELIVERY = "dateDelivery";
    public static int COL_DATEDELIVERY_NUM = 3;
    private static String COL_ID_RECEIVER = "idReceiver";
    public static int COL_NAME_LIVREUR_NUM = 4;
    private static String COL_STATE = "state";
    public static int COL_STATE_NUM = 5;    
    private static String COL_LATITUDE = "latitude";
    public static int COL_LATITUDE_NUM = 6;
    private static String COL_LONGITUDE = "longitude";
    public static int COL_LONGITUDE_NUM = 7;
    private static String COL_SIGNATURE = "signature";
    public static int COL_SIGNATURE_NUM = 8;
    private static String COL_COMMENT = "comment";
    public static int COL_COMMENT_NUM = 9;
    private static String COL_UNDELIVERED = "undelivered";
    public static int COL_UNDELIVERED_NUM = 10;
    
    private String idDelivery;
    private int idTour;
    private int idSender;
    private String dateDelivery;
    private int idReceiver;
    private String state;
    private String latitude;
    private String longitude;
    private String signature;
    private String comment;
    private String undelivered;


	public Delivery() {
		super();
		this.idDelivery = "NR";
		this.idTour = 0;
		this.idSender = 0;
		this.dateDelivery = "NR";
		this.idReceiver = 0;
		this.state = STATE_TODELIVER;
		this.latitude = "NR";
		this.longitude = "NR";
		this.signature = "NR";
		this.comment = " ";
		this.undelivered = " ";
	}

	public String getIdDelivery() {
		return idDelivery;
	}

	public void setIdDelivery(String idDelivery) {
		this.idDelivery = idDelivery;
	}

	public int getIdTour() {
		return idTour;
	}

	public void setIdTour(int idTour) {
		this.idTour = idTour;
	}

	public int getIdSender() {
		return idSender;
	}

	public void setIdSender(int i) {
		this.idSender = i;
	}

	public String getDateDelivery() {
		return dateDelivery;
	}

	public void setDateDelivery(String dateDelivery) {
		dateDelivery=DatabaseUtils.sqlEscapeString(dateDelivery);
		this.dateDelivery = dateDelivery.substring(1, dateDelivery.length()-1);
	}

	public int getIdReceiver() {
		return idReceiver;
	}

	public void setIdReceiver(int idReceiver) {
		this.idReceiver = idReceiver;
	}

	public String getState() {
		return state;
	}

	public void setState(String state) {
		this.state = state;
	}

	public String getLatitude() {
		return latitude;
	}

	public void setLatitude(String latitude) {
		this.latitude = latitude;
	}

	public String getLongitude() {
		return longitude;
	}

	public void setLongitude(String longitude) {
		this.longitude = longitude;
	}

	public String getSignature() {
		return signature;
	}

	public void setSignature(String signature) {
		signature=DatabaseUtils.sqlEscapeString(signature);
		this.signature = signature.substring(1, signature.length()-1);
	}

	public String getComment() {
		return comment;
	}

	public void setComment(String comment) {
		comment=DatabaseUtils.sqlEscapeString(comment);
		this.comment = comment.substring(1, comment.length()-1);
	}

	public String getUndelivered() {
		return undelivered;
	}

	public void setUndelivered(String undelivered) {
		this.undelivered = undelivered;
	}
	
	@Override
	public void createTable(SQLiteDatabase db) {
		String CREATE_TABLE = "CREATE TABLE " + TABLE + " ("
				+ COL_ID_DELIVERY + " TEXT PRIMARY KEY , "
			        + COL_ID_TOUR + " INTEGER, "
			        + COL_ID_SENDER + " INTEGER, "
			        + COL_DATEDELIVERY + " TEXT," 
			        + COL_ID_RECEIVER + " INTEGER," 
			        + COL_STATE + " TEXT," 
			        + COL_LATITUDE + " TEXT," 
			        + COL_LONGITUDE + " TEXT," 
			        + COL_SIGNATURE + " BLOB," 
			        + COL_COMMENT + " TEXT, " 
			        + COL_UNDELIVERED + " TEXT);"; 
			        
			        db.execSQL(CREATE_TABLE);
	}
	
	@Override
	public void removeTable(SQLiteDatabase db) {
		db.execSQL("DROP TABLE IF EXISTS " + TABLE);
	}

	@Override
	public void insert() {
        ContentValues val = new ContentValues();
        val.put(COL_ID_DELIVERY, idDelivery);
        val.put(COL_ID_TOUR, idTour);
        val.put(COL_ID_SENDER, idSender);
        val.put(COL_DATEDELIVERY, dateDelivery);
        val.put(COL_ID_RECEIVER, idReceiver);
        val.put(COL_STATE, state);
        val.put(COL_LATITUDE, latitude);
        val.put(COL_LONGITUDE, longitude);
        val.put(COL_SIGNATURE, signature);
        val.put(COL_COMMENT, comment);
        val.put(COL_UNDELIVERED, undelivered);
		if(!getDelivery())
		{
			LivreApplication.getSqliteManager().insert(TABLE, val);
		}

        
		
	}

	@Override
	public void update() {
		// TODO Auto-generated method stub
		ContentValues val = new ContentValues();
        val.put(COL_ID_DELIVERY, idDelivery);
        val.put(COL_ID_TOUR, idTour);
        val.put(COL_ID_SENDER, idSender);
        val.put(COL_DATEDELIVERY, dateDelivery);
        val.put(COL_ID_RECEIVER, idReceiver);
        val.put(COL_STATE, state);
        val.put(COL_LATITUDE, latitude);
        val.put(COL_LONGITUDE, longitude);
        val.put(COL_SIGNATURE, signature);
        val.put(COL_COMMENT, comment);
        val.put(COL_UNDELIVERED, undelivered);
		LivreApplication.getSqliteManager().update(TABLE, val, COL_ID_DELIVERY+" = '"+this.idDelivery+"';");
	}

	@Override
	public void delete() {
		LivreApplication.getSqliteManager().delete(TABLE, COL_ID_DELIVERY + " = " +this.idDelivery);
		
	}
	public static List<Delivery> getDelivery(int idTour)
	{
		Cursor result = LivreApplication.getSqliteManager().rawQuery("SELECT * FROM " + TABLE +" WHERE "+COL_ID_TOUR+" = "+idTour+";" , null);
		List<Delivery> deliveryList = new ArrayList<Delivery>();
        while(result.moveToNext())
        {
            Delivery del = new Delivery();
            del.setIdDelivery(result.getString(COL_ID_DELIVERY_NUM));
            del.setIdTour(result.getInt(COL_ID_TOUR_NUM));
            del.setIdSender(result.getInt(COL_ID_SENDER_NUM));
            del.setDateDelivery(result.getString(COL_DATEDELIVERY_NUM));
            del.setIdReceiver(result.getInt(COL_NAME_LIVREUR_NUM));
            del.setState(result.getString(COL_STATE_NUM));
            del.setLatitude(result.getString(COL_LATITUDE_NUM));
            del.setLongitude(result.getString(COL_LONGITUDE_NUM));
            del.setSignature(result.getString(COL_SIGNATURE_NUM));
            del.setComment(result.getString(COL_COMMENT_NUM));
            del.setUndelivered(result.getString(COL_UNDELIVERED_NUM));
            
            deliveryList.add(del);
        }
        
        result.close();
		return deliveryList;
	}
	public static Delivery getDelivery(String idDelivery)
	{
		Cursor result = null;
        try
        {
            result = LivreApplication.getSqliteManager().rawQuery("SELECT * FROM "+TABLE+" WHERE "+COL_ID_DELIVERY+" = '"+idDelivery+"';" , null);
        }
        catch(Exception e)
        {
            return null;
        }
        if(!result.moveToFirst())
        {
            result.close();
           return null;
        }
        
	        Delivery d = new Delivery();
	        d.setIdDelivery(idDelivery);
	        d.setComment(result.getString(COL_COMMENT_NUM));
	        d.setDateDelivery(result.getString(COL_DATEDELIVERY_NUM));
	        d.setIdReceiver(result.getInt(COL_NAME_LIVREUR_NUM));
	        d.setIdSender(result.getInt(COL_ID_SENDER_NUM));
	        d.setIdTour(result.getInt(COL_ID_TOUR_NUM));
	        d.setLatitude(result.getString(COL_LATITUDE_NUM));
	        d.setLongitude(result.getString(COL_LONGITUDE_NUM));
	        d.setSignature(result.getString(COL_SIGNATURE_NUM));
	        d.setState(result.getString(COL_STATE_NUM));
	        d.setUndelivered(result.getString(COL_UNDELIVERED_NUM));
	        result.close();
			return d;
        }

	
	private Boolean getDelivery(){
        Cursor result = null;
        try
        {
            result = LivreApplication.getSqliteManager().rawQuery("SELECT * FROM "+TABLE+" WHERE "+COL_ID_DELIVERY+" = '"+this.idDelivery+"'" , null);
        }
        catch(Exception e)
        {
            return false;
        }
        if(result.getCount()==0)
        {
            result.close();
           return false;
        }
        else{
	        
	        result.close();
			return true;
        }

	}

}

